const Base = require('./SpiderBaseClass');

const listSize = 30;

class SpiderBiliBili extends Base {

  static firstListPage() {
    return 1;
  }

  static async getListPage(page) {
    const res = await SpiderBiliBili.get(SpiderBiliBili.listPageUrl(page),
      {dataType: 'json'});

    const list = res.data.data.map(room => {
      return {
        anchor: {
          room: room.room_id,
          name: room.owner.name,
          gender: 0,
          avatar: room.owner.face,
        },
        type: room.area_v2_name,
        data: {
          num_watch: SpiderBiliBili.toNumber(room.online),
          num_live: 0,
          num_follower: 0,
          num_gift: 0,
        },
      };
    });
    return {
      getMoreInfo: true,//需要获取单个直播间信息
      list,
      page,
      nextPage: page + 1,
      hasNextPage: list.length >= listSize,
    };
  }

  static async getRoom(room) {
    const [info, score] = await Promise.all([
      SpiderBiliBili.get(
        'https://api.live.bilibili.com/AppRoom/index?device=phone&platform=ios&scale=3&build=10000&room_id=' +
        room.anchor.room,
        {dataType: 'json'}),
      SpiderBiliBili.get(
        'https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=' +
        room.anchor.room, {dataType: 'json'}),
    ]);

    room.type = info.data.data.area_v2_name;
    room.data.num_gift = SpiderBiliBili.toNumber(
      score.data.data.level.anchor_score);
    room.data.num_follower = SpiderBiliBili.toNumber(info.data.data.attention);
  }

  static listPageUrl(page) {
    return 'https://api.live.bilibili.com/mobile/rooms?area_id=null&page=' +
      page + '&device=phone&platform=ios&scale=3&build=10000';
  }

  static roomUrl(id) {
    return 'https://api.live.bilibili.com/AppRoom/index?device=phone&platform=ios&scale=3&build=10000&room_id=' +
      id;
  }
}

module.exports = SpiderBiliBili;